package com.zephyr.io.chatroom;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SocketServer {

    private static final ExecutorService EXECUTOR_SERVICE = Executors.newFixedThreadPool(2 * Runtime.getRuntime().availableProcessors());

    public static final int PORT = 8888;

    public static void main(String[] args) {
        try {
            open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void open() throws IOException {
        ServerSocket serverSocket = new ServerSocket(PORT);
        System.out.println("服务端启动，等待连接.....");
        while (true) {
            final Socket socket = serverSocket.accept();
            System.out.println("新的连接，客户端：" + socket.getRemoteSocketAddress());
            EXECUTOR_SERVICE.execute(new ClientHandler(socket));
        }
    }

}
